package algorithm.middle;

import algorithm.easy.FairCandySwap888;
import pojo.User;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 请你设计一个数据结构，支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。
 * <p>
 * 实现词典类 WordDictionary ：
 * <p>
 * WordDictionary() 初始化词典对象
 * void addWord(word) 将 word 添加到数据结构中，之后可以对它进行匹配
 * bool search(word) 如果数据结构中存在字符串与 word 匹配，则返回 true ；否则，返回  false 。word 中可能包含一些 '.' ，每个 . 都可以表示任何一个字母。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/design-add-and-search-words-data-structure
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
//前缀树
public class WordDictionary211 {
    private DicTree root;

    public WordDictionary211() {
        root = new DicTree();
    }

    public void addWord(String word) {
        root.add(word);
    }

    public boolean search(String word) {
        return root.search(word);
    }

    static class DicTree {
        //此处是否结尾
        boolean work;

        //从该位置往下存储的数组
        DicTree[] array = new DicTree[26];

        public DicTree() {
        }


        public void add(String word) {
            char c = word.charAt(0);
            DicTree dicTree = array[c - 'a'];
            if (dicTree == null) {
                dicTree = new DicTree();
            }
            array[c - 'a'] = dicTree;
            if (word.length() == 1) {
                dicTree.work = true;
            } else {
                dicTree.add(word.substring(1));
            }
        }

        public boolean search(String word) {
            char c = word.charAt(0);
            //最后一位数字
            if (word.length() == 1) {
                if (c == '.') {
                    for (int i = 0; i < 26; i++) {
                        if (array[i] != null && array[i].work) {
                            return true;
                        }
                    }
                } else {
                    if (array[c - 'a'] != null) {
                        return array[c - 'a'].work;
                    }
                }
            } else {
                //中间数字
                if (c == '.') {
                    for (int i = 0; i < 26; i++) {
                        if (array[i] != null) {
                            if (array[i].search(word.substring(1))) return true;
                        }
                    }
                } else {
                    if (array[c - 'a'] != null) {
                        return array[c - 'a'].search(word.substring(1));
                    }
                }
            }
            return false;
        }
    }

    public static void main(String[] args) {
        User zs = new User("张三", 0);
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        executorService.submit(()->{
            while (true){
                System.out.println(zs.age);
                Thread.sleep(1000);
            }
        });
        while (true){
            zs.setAge(zs.getAge()+1);
        }
    }


    public static double aa(){
        int[] arr = new int[]{1, 2, 3};
        int a = 0;
        a = arr[3];
        return a;
    }

}
